<template>
  <el-row :gutter="24">
    <el-col :span="6">
      <el-card class="box-card">
        <div  class="clearfix" >
          <a :class="{second:real}" class="first"  @click="getShowall()" style="font-size:20px;color:#000;text-decoration:none; cursor: pointer;" >显示所有</a>
        </div>
        <div style="height:200px;overflow-y: auto;-ms-overflow-style: none;overflow: -moz-scrollbars-none;overflow-x:hidden; " class="over_y">
          <el-collapse accordion   v-model="activeNames" @change="handleChange" >
            <el-collapse-item v-for="(item,index) in list" :key="index" :name="item.id" >
              <template slot="title" >
                <i class="header-icon el-icon-info"></i><span class="titleName"  :style="activeNames==item.id? ' margin-left: 110px;color:#3cc1dd;' : ' margin-left: 110px;'"  >{{item.name}}</span>
              </template>
              <a  >
                <li  v-for="(item,index) in item.children" :key="index" :name="item.id" style="list-style:none;margin-left: 50px;margin-top: 10px;" >
                  <span @click="DeHandle(item.id)" style=" cursor: pointer;">{{item.name}}--({{item.phone}})</span>
                </li >
              </a>
            </el-collapse-item>
          </el-collapse>
        </div>
      </el-card>
    </el-col>
    <el-col :span="18">
      <div class="mod-config">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
            <el-button  type="primary" @click="addOrUpdateHandle()">新增</el-button>
            <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            prop="id"
            header-align="center"
            align="center"
            label="编号">
            <template slot-scope="scope">
              {{scope.$index+1}}
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            header-align="center"
            align="center"
            label="联系名称" width="100px" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="email"
            header-align="center"
            align="center"
            label="邮箱" width="150px" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="phone"
            header-align="center"
            align="center"
            label="联系电话" width="150px" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column
            prop="realName"
            header-align="center"
            align="center"
            label="真实姓名">
          </el-table-column>
          <el-table-column
            prop="sex"
            header-align="center"
            align="center"
            label="性别">
            <template slot-scope="scope">
              {{scope.row.sex == 1 ? "男" : "女"}}
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="headImgs"
            header-align="center"
            align="center"
            label="头像">
          </el-table-column> -->
          <el-table-column
            prop="status"
            header-align="center"
            align="center"
            label="状态">
            <template slot-scope="scope">
              {{scope.row.status == 1 ? "启用" : "禁用"}}
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"

                :active-value="1"
                :inactive-value="0"
                @change="handleSwitch(scope.row)">
              </el-switch>
              <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[5, 8, 10, 15]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
        <detils v-if="addOrUpdateVisible" ref="addOrUpdate1" @refreshDataList="getDataList"></detils>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import AddOrUpdate from './smsgcontact-add-or-update'
import Detils from './smsgcontact-detils'
export default {
  data () {
    return {
      dataForm: {
        key: ''
      },
      value:'100',
      activeNames: '',
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      list:[],// 存放分组
      real:true
    }
  },
  components: {
    AddOrUpdate,
    Detils
  },
  activated () {
    this.getDataList()

  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.getgroup()
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/smsgcontact/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key,
          id:sessionStorage.getItem("group_id")
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.records
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    //显示所有
    getShowall(){
      sessionStorage.setItem("group_id","")
      this.real=true
      this.getDataList()
    },
    //获取组
    getgroup () {
      this.$http({
        url: this.$http.adornUrl('/smsggroup/group'),
        method: 'get',
        params: this.$http.adornParams({
          // 'key': this.dataForm.key
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.list = data.list
        } else {
        }
      })
    },
    //禁用启用
    handleSwitch(val){
      console.log(val.id)
      console.log(val.status)
      this.$http({
        url: this.$http.adornUrl(`/smsgcontact/status/${val.id}/${val.status}`),
        method: 'get',
        data: this.$http.adornParams({

        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,

          })
        } else {
        }
      })
    },
    //获取组id
    handleChange(val) {
      console.log(val+"xxxxxxx");
      if(val!=''){
        this.real=false
        sessionStorage.setItem("group_id", val);
        this.$http({
          url: this.$http.adornUrl('/smsgcontact/groupById'),
          method: 'post',
          data: this.$http.adornParams({
            id:val
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.list
          } else {
          }
        })
      }else{
        sessionStorage.setItem("group_id", "");
      }


    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    DeHandle(id){
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate1.init(id)
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/smsgcontact/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
<style scoped>
li{
  list-style:none;
  margin-left: 50px;
  margin-top: 10px;
}
.titleName{
  margin-left: auto;

}
.clearfix{
  text-align: center;
}
a{
  text-decoration:none;
}
.second{
  color:#3cc1dd !important;
}
.over_y::-webkit-scrollbar {display:none}

</style>
